﻿using ConsoleApp2.Themeup;
using FourOperationsDemo.BaseDate;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace ConsoleApp2.Base
{
    public class OneTheme : IBasicTheme
    {
        readonly Random random = new Random();

        public string Add { get; set; }
        public int DM { get; set; }
        public string Sub { get; private set; }
        private readonly string[] vsArray1 = new string[10];
        /// <summary>
        /// 一年级出题范围：20以内加减
        /// </summary>
        public OneTheme()
        {
            Randomnum();
        }
        public void Randomnum()
        {
            BasicDataA = random.Next(0, 20);
            BasicDataB = random.Next(0, 20);
        }


        public string AddTheme()
        {
            try
            {
                if (BasicDataA > 20 && BasicDataB > 20)
                {
                    throw new Exception("A>20 并且 B>20");
                }
                else
                {
                    Add = BasicDataA + "+" + BasicDataB;
                    DM = BasicDataA + BasicDataB;
                    return Add;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return Add = null;

        }
        public string SubTheme()
        {
            try
            {
                if (BasicDataA > 20 && BasicDataB > 20)
                {
                    throw new Exception("A>20 并且 B>20");
                }
                else
                {
                    if (BasicDataA > BasicDataB)
                    {
                        Sub = BasicDataA + "-" + BasicDataB;
                    }
                    else
                    {
                        BasicDataA += BasicDataB;
                        BasicDataB = BasicDataA - BasicDataB;
                        BasicDataA -= BasicDataB;
                        Sub = BasicDataA + "-" + BasicDataB;
                    }
                    DM = BasicDataA + BasicDataB;
                    return Sub;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return Sub = null;





        }


        /// <summary>
        ///      ///调用此方法一次出10题加法题目
        /// </summary>
        public override string AddThemeCount()
        {
            string[] Array1 = new string[10];

            for (int i = 0; i < Array1.Length; i++)
            {
                var oneTheme = new OneTheme();
                oneTheme.AddTheme();
                Array1[i] = oneTheme.Add;
                vsArray1[i] = oneTheme.Add;

            }
            //for (int i = 0; i < Array1.Length; i++)
            //{
            //    Console.WriteLine(Array1[i]);
            //}

            return Array1[9];

        }
        /// <summary>
        ///  //调用此方法一次出10题减法题目
        /// </summary>
        public override string SubThemeCount()
        {
            string[] Array1 = new string[10];

            for (int i = 0; i < Array1.Length; i++)
            {
                var oneTheme = new OneTheme();
                oneTheme.SubTheme();
                Array1[i] = oneTheme.Sub;

            }
            //for (int i = 0; i < Array1.Length; i++)
            //{
            //    Console.WriteLine(Array1[i]);
            //}
            return Array1[9];
        }

        //创建新文件
        //第一个是路径，
        //第二个是确定当文件存在时，是覆盖掉还是将数据追加到文件中，true是将数据追加到已存在文件中，false是覆盖已存在的文件。
        //第三个参数是要使用的字符编码，一般是使用默认的。
        //StreamWriter writer = new StreamWriter(result, false, Encoding.Default);
        //writer.Flush();



        /// <summary>
        /// ////存在无法输出bug ，已经修复可以抓取数据  vsArray1[i]
        /// </summary>
        public void AddOutPutTxt()
        {
            string result = @"F:\demo.txt";//保存文件路径
            OneTheme oneTheme = new OneTheme();
            oneTheme.AddThemeCount();
            //定义写文件流
            FileStream fsw = new FileStream(result, FileMode.OpenOrCreate);
            //写入的内容            
            StreamWriter wr = new StreamWriter(fsw);//                    
            for (int i = 0; i < vsArray1.Length; i++)
            {
                wr.Write(oneTheme.vsArray1[i]);
            }
            //关闭文件流
            fsw.Close();
        }
    }
}
